BEGIN TRANSACTION;
BEGIN TRY
    -- 检查并扣减库存（带锁）
    UPDATE Products WITH (UPDLOCK, ROWLOCK)
    SET Stock = Stock - @Quantity
    WHERE ProductID = @ProductID AND Stock >= @Quantity;

    -- 生成订单
    INSERT INTO Orders (ProductID, Quantity) 
    VALUES (@ProductID, @Quantity);

    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    IF XACT_STATE() <> 0 
        ROLLBACK TRANSACTION;
    RAISERROR('库存不足或系统异常', 16, 1);  -- 明确错误提示‌:ml-citation{ref="3,7" data="citationList"}
END CATCH;
